var rowCounter = $("#new-bar-grid").length || 0;
rowCounter++;

function setContent(contentPage){
    $("#content").load(contentPage);
}

function getClientInfo(){
    var dfd = new $.Deferred();
    jQuery.ajax({ 
        url: '//freegeoip.net/json/', 
        type: 'POST', 
        dataType: 'jsonp',
        success: function(location) {
          // example where I update content on the page.
          /*jQuery('#city').html(location.city);
          jQuery('#region-code').html(location.region_code);
          jQuery('#region-name').html(location.region_name);
          jQuery('#areacode').html(location.areacode);
          jQuery('#ip').html(location.ip);
          jQuery('#zipcode').html(location.zipcode);
          jQuery('#longitude').html(location.longitude);
          jQuery('#latitude').html(location.latitude);
          jQuery('#country-name').html(location.country_name);
          jQuery('#country-code').html(location.country_code);*/
          dfd.resolve(location);
        }
    });
    return dfd.promise();
}

function deleteRow(r, gridId){
    console.log(gridId);
    var i = r.parentNode.parentNode.rowIndex;
    document.getElementById(gridId).deleteRow(i);
}

function addBarToGrid(gridId){
    switch(gridId){
        case("new-bar-grid"):
            var html = "<tr id=row-" + rowCounter + "><td>" + $("#name").val() + "</td>";
            html += "<td>" + $("#country").val("Norway") + "</td>";
            html += "<td>" + $("#city").val() + "</td>";
            html += "<td>" + $("#postal-no").val() + "</td>";
            html += "<td>" + $("#address").val() + "</td>";
            html += "<td>" + $("#cc").val() + "</td>";
            html += "<td>" + $("#beer-price").val() + "</td>";
            html += "<td>" + $("#description").val() + "</td>";
            html += "<td>" + $("#age-limit").val() + "</td>";
            html += "<td>" + "<input class='delete-row-btn btn' type='button' onclick='deleteRow(this, 'new-bar-grid')' title='Delete row'>" + "</td>";
            html += "</tr>";
            break;
        case("new-user-grid"):
            var html = "<tr id=row-" + rowCounter + "><td>" + $("#username").val() + "</td>";
            html += "<td>" + $("#email").val() + "</td>";
            html += "<td>" + $("#firstname").val() + "</td>";
            html += "<td>" + $("#lastname").val() + "</td>";
            html += "<td>" + $("#country").val() + "</td>";
            html += "<td>" + "<input class='delete-row-btn btn' type='button' onclick='deleteRow(this, 'new-user-grid')' title='Delete row'>" + "</td>";
            html += "</tr>";
            break;
        default:
            break;
    }
    $("#" + gridId).append(html);
     reset("#" + gridId);
     rowCounter++;
}

function reset(gridId){
    switch(gridId){
        case ("new-bar-grid"):
            $("#name").val("");
            $("#country").val("Norway");
            $("#city").val("");
            $("#postal-no").val("");
            $("#address").val("");
            $("#cc").val("0");
            $("#beer-price").val("");
            $("#description").val("");
            $("#age-limit").val("");
            break;
        case ("new-user-grid"):
            $("#username").val("");
            $("#email").val("");
            $("#firstname").val("");
            $("#lastname").val("");
            $("#country").val("Norway");
        default:
            break;
    }
}

function validate(){
    var isValid = true;
    if(!$("#name").val()){
        isValid = false;
    } else if(!$("#country").val()){
        isValid = false;
    } else if(!$("#city").val()){
        isValid = false;
    } else if(!$("#postal-no").val()){
        isValid = false;
    } else if(!$("#address").val()){
        isValid = false;
    } else if(!$("#beer-price").val()){
        isValid = false;
    } else if(!$("#description").val()){
        isValid = false;
    } else if(!$("#age-limit").val()){
        isValid = false;
    }
    return isValid;
}